import { Reducer, Subscription, Effect, history } from 'umi';
import moment from 'moment';

import {
    requestOrderDetail,
    postOrderDetail,
} from '@/pages/order_detail/server';

export interface OrderModelState {
    id: string; //订单编号
    company: string; //维修单位
    personofrepair: string; //通知维修人
    phone: string; //手机号码
    notice_time: string; //通知时间
    crash: number; //紧急程度
    content: string; //维修内容
    completedTime: string; //要求完成时间
    status: number; //维修结果
    color: string; //颜色
    estimated_finish_time: string; //预定完成时间
    service_time: string; //服务时间
    incompleteReason: string; //未完成原因
    finishImages: []; //服务完成照片
    score: number; //评分
    comment: string; //评价
}

export interface OrderModelType {
    namespace: 'order';
    state: OrderModelState;
    reducers: {
        save: Reducer<OrderModelState>;
        reset: Reducer<OrderModelState>;
    };
    effects: {
        query: Effect;
        post: Effect;
    };
}

const UserModel: OrderModelType = {
    namespace: 'order',
    state: {
        id: '', //订单编号
        company: '', //维修单位
        personofrepair: '', //通知维修人
        phone: '', //手机号码
        notice_time: '', //通知时间
        crash: 1, //紧急程度
        content: '', //维修内容
        completedTime: '', //要求完成时间
        status: 1, //维修结果
        color: '', //颜色
        estimated_finish_time: '', //预定完成时间
        service_time: '', //服务时间
        incompleteReason: '', //未完成原因
        finishImages: [], //服务完成照片
        score: 1, //评分
        comment: '', //评价
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
        reset(state) {
            return {
                ...state,
                id: '', //订单编号
                company: '', //维修单位
                personofrepair: '', //通知维修人
                phone: '', //手机号码
                notice_time: '', //通知时间
                crash: 1, //紧急程度
                content: '', //维修内容
                completedTime: '', //要求完成时间
                status: 1, //维修结果
                color: '', //颜色
                estimated_finish_time: '', //预定完成时间
                service_time: '', //服务时间
                incompleteReason: '', //未完成原因
                finishImages: [], //服务完成照片
                score: 1, //评分
                comment: '', //评价
            };
        },
    },
    effects: {
        *query({ payload }, { call, put }) {
            const response = yield call(requestOrderDetail, payload);
            if (response.status === 0) {
                yield put({
                    type: 'save',
                    payload: response.data,
                });
            }
        },
        *post({ payload }, { call, put }) {
            const response = yield call(postOrderDetail, payload);
            if (response.status === 0) {
                yield put({
                    type: 'save',
                    payload: response.data,
                });
                history.push(`/order_detail?id=${response.data.id}`);
            }
        },
    },
};

export default UserModel;
